﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.VisualStudio.TemplateWizard;
using EnvDTE;

namespace CodeCounsel.SharePoint.DevTools.ProjectTypes
{
    abstract class BaseWizard : IWizard
    {
        ICertificateGenerator _certificateGenerator;

        public BaseWizard()
            : this(new DefaultCertificateGenerator())
        {
        }

        public BaseWizard(ICertificateGenerator certificateGenerator)
        {
            _certificateGenerator = certificateGenerator;
        }

        public void RunStarted(object automationObject, Dictionary<string, string> replacementsDictionary, WizardRunKind runKind, object[] customParams)
        {
            DTE dte = automationObject as DTE;
            if (dte == null)
            {
                throw new ArgumentNullException("automationObject");
            }
            WizardUtilities.Dte = dte;
            if (WizardUtilities.IsSharePointInstalled(dte) == false)
            {
                throw new WizardCancelledException("SharePoint is not installed.");
            }
            WizardUtilities.CheckPermissions();
            InitializeFromWizardData(replacementsDictionary);
            DisplayWizardForm(dte);
            PopulateReplacementDictionary(replacementsDictionary);
            _certificateGenerator.GenerateKeyFile(replacementsDictionary);
        }

        public void BeforeOpeningFile(ProjectItem projectItem)
        {
        }
        
        public void ProjectFinishedGenerating(Project project)
        {
            RunProjectFinishedGenerating(project);
        }

        public void ProjectItemFinishedGenerating(ProjectItem projectItem)
        {
            RunProjectItemFinishedGenerating(projectItem);
        }

        public void RunFinished()
        {
            RunWizardFinished();
        }

        public virtual void RunProjectFinishedGenerating(Project project)
        {
            SetProjectProperties(project);
            _certificateGenerator.AddKeyFile(project);
        }

        public virtual void RunWizardFinished()
        {
        }

        public virtual void RunProjectItemFinishedGenerating(ProjectItem projectItem)
        {
        }
        
        public virtual bool ShouldAddProjectItem(string filePath)
        {
            return true;
        }

        public static SharePointServices GetSharePointServices(Project project)
        {
            return new SharePointServices(project);
        }

        protected virtual void SetProjectProperties(Project project)
        {
        }
        protected virtual IWizardFormExtension CreateWizardForm(DTE dte)
        {
            return null;
        }
        protected virtual void InitializeFromWizardData(Dictionary<string, string> replacementsDictionary)
        {
        }
        protected virtual void PopulateReplacementDictionary(Dictionary<string, string> replacementsDictionary)
        {
        }

        void DisplayWizardForm(DTE dte)
        {
            IWizardFormExtension extension = CreateWizardForm(dte);
            if (extension != null)
            {
                extension.Launch();
                if (extension.Cancelled)
                {
                    throw new WizardCancelledException();
                }
            }
        }
    }
}
